//
//  foundQuestFooterView.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/8/2.
//

#import "foundQuestFooterView.h"
#import "userPhoneView.h"
#import "YTBaseSelectStringView.h"
#import "TemplateFoundDetailsModel.h"

@interface foundQuestFooterView ()<UITextViewDelegate>

@property (nonatomic ,strong) UILabel * name;
@property (nonatomic ,strong) UILabel * StrLab;
@property (nonatomic ,strong) IQTextView * textview;
@property (nonatomic ,strong) UIView * topBGView;

///任务指派
@property (nonatomic ,strong) UIView * appointorView;
@property (nonatomic ,strong) userPhoneView * phone1;
@property (nonatomic ,strong) userPhoneView * phone2;
@property (nonatomic ,strong) userPhoneView * phone3;
@property (nonatomic ,strong) userPhoneView * phone4;
@property (nonatomic ,strong) userPhoneView * phone5;
@property (nonatomic ,strong) userPhoneView * phone6;
@property (nonatomic ,strong) userPhoneView * phone7;
@property (nonatomic ,strong) userPhoneView * phone8;

@property (nonatomic ,strong) NSArray< userPhoneView * > *phoneArray;
@property (nonatomic ,strong) YTBaseSelectStringView * timeview;

@end
@implementation foundQuestFooterView

+ (instancetype)NewView {
    foundQuestFooterView * view = [foundQuestFooterView new];
    view.clipsToBounds = YES;
    view.backgroundColor = UIColor.clearColor;
    [view newUI];
    return view;
}
- (void)newUI{
    
    UIView * backview = [UIView new];
    backview.backgroundColor = UIColor.whiteColor;
    [self addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.offset(0);
        make.bottom.offset(kwt(-10));
    }];
    
    UIView * topBGView= [UIView new];
    self.topBGView = topBGView;
    [backview addSubview:topBGView];
    [DTKTool YJBK:kwt(5) andView:topBGView andWidth:0.5 andColor:KcolorHex(@"#D5D5D5")];
    [topBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(-10));
        make.right.offset(kwt(-10));
        make.left.offset(kwt(10));
        make.height.offset(kwt(10));
    }];
    
    UIView * makeview = [UIView new];
    makeview.backgroundColor = KcolorHex(@"#F6F7F9");
    [DTKTool YJ:kwt(5) andView:makeview];
    [backview addSubview:makeview];
    [makeview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topBGView.mas_bottom).offset(kwt(10));
        make.right.offset(kwt(-10));
        make.left.offset(kwt(10));
        make.height.offset(kwt(135));
    }];
    
    UILabel * name = [DTKTool labelWithText:@"任务说明" andFont:kCFont(18) andTextColor:KcolorHex(@"#222222")];
    [makeview addSubview:name];
    self.name = name;
    [name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.top.offset(kwt(14));
    }];
    
    UILabel * StrLab = [DTKTool labelWithText:@"0/140" andFont:kFont(17) andTextColor:KcolorHex(@"#787A79")];
    [makeview addSubview:StrLab];
    self.StrLab = StrLab;
    [StrLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-12));
        make.centerY.equalTo(name);
    }];
    
    UIView * line = [UIView new];
    line.backgroundColor = KcolorHex(@"#DBDBDB");
    [makeview addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.right.offset(kwt(-10));
        make.top.offset(kwt(40));
        make.height.offset(0.5);
    }];
    
    
    IQTextView * textview = [IQTextView new];
    textview.textColor = KcolorHex(@"#222222");
    textview.font = kFont(17);
    textview.delegate = self;
    textview.backgroundColor = UIColor.clearColor;
    self.textview = textview;
    [makeview addSubview:textview];
    [textview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.right.offset(kwt(-10));
        make.top.offset(kwt(50));
        make.bottom.offset(kwt(-10));
    }];
    
    UIView * appointorView = [UIView new];
    self.appointorView = appointorView;
    appointorView.backgroundColor = KcolorHex(@"#F6F7F9");
    [DTKTool YJ:kwt(5) andView:appointorView];
    [backview addSubview:appointorView];
    [appointorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(makeview.mas_bottom).offset(kwt(10));
        make.right.offset(kwt(-10));
        make.left.offset(kwt(10));
        make.height.offset(kwt(225));
    }];
    
    
    UILabel * appointorlab = [DTKTool labelWithText:@"*任务指派人" andFont:kCFont(17) andTextColor:KcolorHex(@"#222222")];
    appointorlab.attributedText = [DTKTool AttributedStringWithStr:@"*任务指派人" andNodeColor:KcolorHex(@"#222222") andSeledColor:KcolorHex(@"#FA0F0F") andFont:kCFont(17) andPattenr:@"[*]"];
    [appointorView addSubview:appointorlab];
    [appointorlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.top.offset(kwt(15));
    }];
    
    userPhoneView * phone1 = [self phoneviewWithIndex:0];
    self.phone1 = phone1;
    [appointorView addSubview:phone1];
    [phone1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(80));
        make.top.offset(kwt(45));
        make.left.offset(kwt(10));
    }];
    
    userPhoneView * phone2 = [self phoneviewWithIndex:1];
    self.phone2 = phone2;
    [appointorView addSubview:phone2];
    [phone2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(80));
        make.top.equalTo(phone1);
        make.left.equalTo(phone1.mas_right).offset(kwt(7));
    }];
    
    
    userPhoneView * phone3 = [self phoneviewWithIndex:2];
    self.phone3 = phone3;
    [appointorView addSubview:phone3];
    [phone3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(80));
        make.top.equalTo(phone2);
        make.left.equalTo(phone2.mas_right).offset(kwt(7));
    }];
    
    
    userPhoneView * phone4 = [self phoneviewWithIndex:3];
    self.phone4 = phone4;
    [appointorView addSubview:phone4];
    [phone4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(80));
        make.top.equalTo(phone3);
        make.left.equalTo(phone3.mas_right).offset(kwt(7));
    }];
    
    userPhoneView * phone5 = [self phoneviewWithIndex:4];
    self.phone5 = phone5;
    [appointorView addSubview:phone5];
    [phone5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(80));
        make.left.equalTo(phone1);
        make.top.equalTo(phone1.mas_bottom).offset(kwt(12));
    }];
    
    userPhoneView * phone6 = [self phoneviewWithIndex:5];
    self.phone6 = phone6;
    [appointorView addSubview:phone6];
    [phone6 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(80));
        make.top.equalTo(phone5);
        make.left.equalTo(phone5.mas_right).offset(kwt(7));
    }];
    
    userPhoneView * phone7 = [self phoneviewWithIndex:6];
    self.phone7 = phone7;
    [appointorView addSubview:phone7];
    [phone7 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(80));
        make.top.equalTo(phone6);
        make.left.equalTo(phone6.mas_right).offset(kwt(7));
    }];
    
    userPhoneView * phone8 = [self phoneviewWithIndex:7];
    self.phone8 = phone8;
    [appointorView addSubview:phone8];
    [phone8 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(80));
        make.top.equalTo(phone7);
        make.left.equalTo(phone7.mas_right).offset(kwt(7));
    }];
    
    self.phoneArray = @[self.phone1,self.phone2,self.phone3,self.phone4,self.phone5,self.phone6,self.phone7,self.phone8];
    
    UIView * timebackview = [UIView new];
    [backview addSubview:timebackview];
    [timebackview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(appointorView.mas_bottom).offset(kwt(5));
        make.left.offset(kwt(10));
        make.right.offset(kwt(-10));
        make.height.offset(kwt(65));
    }];
    YTBaseSelectStringView * timeview = [YTBaseSelectStringView NewView];
    self.timeview = timeview;
    timeview.name = @"任务执行时间";
    timeview.forceFlag = YES;
    MJWeakSelf
    timeview.block = ^(NSString * _Nonnull content) {
        if(weakSelf.openTimeBlock){
            weakSelf.openTimeBlock(weakSelf.model);
        }
    };
    [timebackview addSubview:timeview];
    [timeview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.offset(0);
    }];
    
    UIView * buttomview = [UIView new];
    [self addSubview:buttomview];
    buttomview.backgroundColor = KcolorHex(@"#EBEDF0");
    [buttomview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.left.offset(0);
        make.height.offset(kwt(10));
    }];
}

- (userPhoneView *)phoneviewWithIndex:(int)index{
    userPhoneView * phoneview = [userPhoneView NewView];
    MJWeakSelf
    phoneview.deleteBlock = ^{
        [weakSelf.model.appointorArray removeObjectAtIndex:index];
        if(weakSelf.reLoadBlock){
            weakSelf.reLoadBlock();
        }
    };
    phoneview.openImgBlock = ^{
        if(weakSelf.openUserBlock){
            weakSelf.openUserBlock(weakSelf.model);
        }
    };
    phoneview.addImgBlock = ^{
        if(weakSelf.openUserBlock){
            weakSelf.openUserBlock(weakSelf.model);
        }
    };
    return phoneview;
}


- (void)setModel:(TemplateFoundDetailsTaskTypeModelListModel *)model{
    _model = model;
    if(model.isSQ){
        return;
    }
    self.StrLab.text = [NSString stringWithFormat:@"%lu/140",(unsigned long)model.Description.length];
    self.textview.text = model.Description;
    self.timeview.content = model.executeTime;
    [self.topBGView mas_updateConstraints:^(MASConstraintMaker *make) {
        if(model.isBG){
            make.height.offset(kwt(15));
        } else {
            make.height.offset(0);
        }
    }];
    
    for (int i = 0; i < 8; i++) {
        if(model.appointorArray.count > i){
            self.phoneArray[i].hidden = NO;
            if(model.appointorArray[i].userIdentity == 1){///管理员
                [self.phoneArray[i] setImg:[UIImage imageNamed:@"task_assigner_defaul_avatar"]];
            } else {
                [self.phoneArray[i] setImg:[UIImage imageNamed:@"task_assigner_defaul_staff"]];
            }
            [self.phoneArray[i] setContent:model.appointorArray[i].realName];
        } else  if(model.appointorArray.count == i){
            self.phoneArray[i].hidden = NO;
            [self.phoneArray[i] deleteImg];
        } else {
            self.phoneArray[i].hidden = YES;
        }
    }
    [self.appointorView mas_updateConstraints:^(MASConstraintMaker *make) {
        if(model.appointorArray.count > 3){
            make.height.offset(kwt(225));
        } else {
            make.height.offset(kwt(135));
        }
    }];
   
}
-(void)textViewDidChange:(UITextView *)textView{
    if(textView.text.length > 140){
        textView.text = [textView.text substringToIndex:140];
    }
    self.StrLab.text = [NSString stringWithFormat:@"%lu/140",(unsigned long)textView.text.length];
}
- (void)textViewDidEndEditing:(UITextView *)textView {
    self.model.Description = textView.text;
}


@end
